<?
session_start();
include "adm_header.php"; 
writeCommonAdminTable(LA_HTML_EDITOR,1);
$c = 0;
$num_us = 0;
$paid = "";

if (!$set_template_dir)
	$set_template_dir="templates_old";
	
if (isset($_SESSION["set_template_dir"]))
	$set_template_dir=$_SESSION["set_template_dir"];
$aArray["Editor"] = "adm_html_edit.php";
$path_original = "$set_path/admin/".$set_template_dir."_src";
$path_changed = "$set_path/admin/config/$set_template_dir";

echo "<p>".LA_HTML_DESCR1."</p>";
echo "<p>".LA_HTML_DESCR2."</p>";



$dir = opendir($path_original);
$edit_file=getParam("edit_file","");

if (getParam("del","")  && WA())
{
	@unlink("$path_changed/" . getParam("del",""));
	echo "<p><b>".LA_HTML_SUCCESS_TITLE."</b><br />".LA_HTML_SUCCESS_MORE."</p>";
}

echo "<form method='post' action='".$aArray["Editor"]."'>";
print "<select name='edit_file'>";
while ($file = readdir($dir))
{
        if ($file <> "." AND $file <> ".." AND $file <> "" AND $file <> "changed")
        {
        	$mod = "";
        	
        	if (file_exists("$path_changed/$file"))
        		$mod = "*";
        	
        	
        	print "<option value='$file'";
        		
        	if ($edit_file == $file)
        	{
          	print " selected";
	        }	
	 	      print ">$file $mod</option>";
	 	      
        }
        
}
closedir($dir);
print "</select>";
print " <input type='submit' name='edit' value='".LA_HTML_LOAD."'>";
echo "</form>";


if (isset($_REQUEST["save"]))
{
	$edit_file=getParam("edit_file","");
	$contents=$_REQUEST["contents"];
	if (!$set_demo)
	{
	
	if (!empty($contents))
	{
		if (!WA())
		{
			echo "<p><b>Demo-mode</b><br>System in demo-mode, no configuration saved!<br></p>";	
		}
		else 
		{
			$file_pointer = fopen("$path_changed/$edit_file", "w+");
			$contents = ereg_replace('\\\\','', $contents);
			fwrite($file_pointer, $contents);
			chmod("$path_changed/$edit_file",octdec($set_chmod));
	  		fclose($file_pointer);
	  		
	  		echo "<p>".LA_HTML_SAVED ." " . "$edit_file</p>";
	  		rm($set_path . "/admin/config/cache/");
	  		rm($set_path . "/admin/config/templates_c");
		}
	}
	else 
	{
		echo "<p><font color='red'>Warning!</font><br>You tried to save a empty file. In order to protect from loosing you changes due to a browser error or any other error, you need to manually empty the /changed template if you really wanted to wipe out your custom template.</p>";	
	}
	}
	else 
	{
		echo "<p><font color='red'>System in demo-mode!</font><br>No changes was saved.</p>";	

	}
 
	
}



if (isset($_REQUEST["edit_file"])  && WA())
{
	$edit_file= $_REQUEST["edit_file"];
	echo "<b>".LA_HTML_NOW_EDITING.": " . $_REQUEST["edit_file"] ."</b><br />";
	
	if (file_exists("$path_changed/$edit_file"))
	{
		$edit_file_wo=ereg_replace("\.tpl","",$edit_file);
		echo "<br /><a href='?del=$edit_file'><u>".LA_HTML_REVERT."</u></a> | <a href='adm_viewsource.php?file=$edit_file_wo' target='new'><u>".LA_HTML_VIEW_ORIGINAL."</u></a><br>";
	}
	if (!file_exists($path_changed))
		mkdir($path_changed,octdec($set_chmod));
		
	

	if (!is_writeable($path_changed))
	{
		echo "Warning if you are on Linux: The path $path_changed is not writeable!";	
	}
	
	echo "<form method='post' action='".$aArray["Editor"]."'>";
	echo "<input type='hidden' name='edit_file' value='".$_REQUEST["edit_file"]."'>";
	
	if (file_exists("$path_changed/$edit_file"))
	{
    	$filename = "$path_changed/$edit_file"; 		
    }
    else 
    {	
    	$filename = "$path_original/$edit_file";
    }
		
	$handle = fopen ($filename, "r");
	$contents = fread ($handle, filesize ($filename));
	
	$contents=stripslashes($contents);
	echo "<textarea rows='30' id='contents' name='contents' cols='100'>".encodeHTML($contents)."</textarea>";
	
	fclose ($handle);
	print "<br><input type='submit' name='save' value='".LA_HTML_SAVE."'>";
	echo "</form>";	
	
	
	
	
}

writeCommonAdminTable("","");
require "adm_footer.php"; ?>